
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IOutmatchBO;
import com.wyzz.hr.dao.IOutmatchDAO;
import com.wyzz.hr.pojo.Iodef;
import com.wyzz.hr.pojo.Outmatch;
import com.wyzz.hr.pojo.OutmatchBasic;
import com.wyzz.hr.pojo.OutmatchModel;
import com.wyzz.hr.util.output.FieldOperate;


public class OutmatchBOImpl implements IOutmatchBO {

	public OutmatchBOImpl() {
	}

	public IOutmatchDAO getDao() {
		return dao;
	}

	public void setDao(IOutmatchDAO dao) {
		this.dao = dao;
	}

	public Outmatch loadObject(String id) {
		return loadObject(id, null);
	}

	public Outmatch loadObject(String id, String fetch[]) {
		return (Outmatch) dao.loadObject(com.wyzz.hr.pojo.Outmatch.class, id,
				fetch, new boolean[0]);
	}

	public boolean saveObject(Outmatch obj) {
		dao.saveObject(obj);
		return true;
	}

	public boolean updateObject(Outmatch obj) {
		dao.updateObject(obj);
		return true;
	}

	public boolean saveOrUpdateList(List list) {
		dao.saveOrupdate(list);
		return true;
	}

	public boolean deleteObject(String id) {
		dao.deleteObject(com.wyzz.hr.pojo.Outmatch.class, id);
		return true;
	}

	public Iodef loadIodefByName(String name) {
		return (Iodef) dao.loadObjectByName(com.wyzz.hr.pojo.Iodef.class,
				Iodef.PROP_IO_NAME, name, null, new boolean[0]);
	}

	public List getOutmatchList(OutmatchModel outmatchModel) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Outmatch.class);
		detachedCriteria.setFetchMode(Outmatch.PROP_OM_OMB, FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq(Outmatch.PROP_OM_OMM,
				outmatchModel));
		detachedCriteria.addOrder(Order.asc(Outmatch.PROP_OM_SORT_ID));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	public List getFullOutmatchList(OutmatchModel outmatchModel) {
		List result = getOutmatchList(outmatchModel);
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.OutmatchBasic.class);
		detachedCriteria.add(Restrictions.eq(OutmatchBasic.PROP_OMB_IO,
				outmatchModel.getOmmIo()));
		detachedCriteria.addOrder(Order.asc(OutmatchBasic.PROP_OMB_SORT_ID));
		List ombList = dao.findByCriteria(detachedCriteria);
		Outmatch omTmp;
		for (Iterator i$ = result.iterator(); i$.hasNext(); omTmp
				.setOmIsOutput(Integer.valueOf(1))) {
			omTmp = (Outmatch) i$.next();
			ombList.remove(omTmp.getOmOmb());
		}

		int sortId = ((Outmatch) result.get(result.size() - 1)).getOmSortId()
				.intValue() + 1;
		for (Iterator i$ = ombList.iterator(); i$.hasNext(); result.add(omTmp)) {
			OutmatchBasic ombTmp = (OutmatchBasic) i$.next();
			omTmp = new Outmatch();
			omTmp.initByOutmatchBasic(ombTmp);
			omTmp.setOmSortId(Integer.valueOf(sortId++));
			omTmp.setOmIsOutput(Integer.valueOf(0));
		}

		return result;
	}

	public List getOutmatchListOrderById(OutmatchModel outmatchModel) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Outmatch.class);
		detachedCriteria.setFetchMode(Outmatch.PROP_OM_OMB, FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq(Outmatch.PROP_OM_OMM,
				outmatchModel));
		detachedCriteria.addOrder(Order.asc(Outmatch.PROP_OM_ID));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	public void initOutmatchMaps(List omList) {
		for (int i = 0; i < omList.size(); i++)
			((Outmatch) omList.get(i)).setAllMaps();

	}

	public List getOutputList(OutmatchModel outmatchModel) {
		List result = new ArrayList();
		List omList = getOutmatchList(outmatchModel);
		if (omList == null || omList.size() < 1)
			return result;
		for (int i = 0; i < omList.size(); i++) {
			Outmatch omTmp = (Outmatch) omList.get(i);
			int groupOrder = omTmp.getOmIsGroup().intValue();
			if (groupOrder != 0)
				groupOrder = (2 - (groupOrder - 1) / 2)
						* ((groupOrder % 2) * 2 - 1);
			result.add(new FieldOperate(omTmp.getOmOmb().getOmbFieldName(),
					omTmp.getOmFieldDesc(), omTmp.getOmOmb().getOmbDataType(),
					omTmp.getOmFormat(), omTmp.getOmStatistic(), omTmp
							.getOmColumnWidth().intValue(), groupOrder));
		}

		return result;
	}

	public List getOutmatchListByOmb(String ombId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Outmatch.class);
		detachedCriteria.add(Restrictions.eq(Outmatch.PROP_OM_OMB,
				new OutmatchBasic(ombId)));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	public List getOmListByOmbFieldNameArr(String ombFieldNameArr[]) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Outmatch.class);
		detachedCriteria.createAlias(Outmatch.PROP_OM_OMB, "omOmb", 1);
		detachedCriteria.add(Restrictions.in("omOmb.ombFieldName",
				ombFieldNameArr));
		List result = dao.findByCriteria(detachedCriteria);
		return result;
	}

	private IOutmatchDAO dao;
}
